# 开局要安装的软件
all_needed_software=( zsh vim samba git openssh-server apt-file curl lua5.1 curl cmake llvm clangd tmux )

# apt源
declare -A apt_source_list_mirror
apt_source_list_mirror['tsinghua']=''
apt_source_list_mirror['ustc']=''

# echo -e使用的彩色
color_red="\033[31m"
color_green="\033[32m"
color_yellow="\033[33m"
color_blue="\033[34m"
color_magenta="\033[35m"
color_skyblue="\033[36m"
color_end='\033[0m'


__change_apt_source_list() {
    name=$(cat /etc/apt/sources.list | grep '^deb' | awk -F' ' '{print $3}' | head -1 )
    echo "\
# 默认注释了源码仓库，如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ ${name} main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ ${name} main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ ${name}-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ ${name}-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ ${name}-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ ${name}-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ ${name}-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ ${name}-backports main restricted universe multiverse

# 预发布软件源，不建议启用
# deb https://mirrors.ustc.edu.cn/ubuntu/ ${name}-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ ${name}-proposed main restricted universe multiverse
" > /etc/apt/sources.list
    echo "更新临时镜像源..."
    apt-get update
    echo "正在安装必要的git..."
    apt-get install -y git
}


change_apt_source_list() {

    # 测速
    echo "正在给镜像源测速..."
    for i in ${!apt_source_list_mirror[@]}; do
        domain_name=$(sed -nE 's#.*http[^/]+//([^/]+)/ubuntu/.*#\1#p' configs/apt_source_list_template/$i.txt | head -1)
        for j in ${domain_name}; do
            apt_source_list_mirror["$i"]=$(ping -c 2 -q $j | awk -F/ '/^rtt/ {print $5}')
        done
    done

    while true; do
        # 引导用户选择镜像源
        echo "which mirror do you want to use:"
        index=0
        for i in ${!apt_source_list_mirror[@]}; do
            echo -e "    ${index}. $i: delay $color_red${apt_source_list_mirror[$i]}$color_end ms"
            index=$(($index + 1))
        done
        read -p "type [0-$(( index - 1 ))]: " anwser
        echo $anwser
        if [[ $anwser -lt 0 ]] || [[ $anwser -ge $index ]]; then
            continue
        fi
        anwsers=( ${!apt_source_list_mirror[@]} )
        anwser=${anwsers[$anwser]}
        
        # 开始设置镜像源
        echo "正在设置选择的镜像源"
        name=$(grep -P "^deb " /etc/apt/sources.list | awk -F' ' '{print $3}' | grep -v '-' | uniq)
        cat configs/apt_source_list_template/$anwser.txt | sed "s#jammy#${name}#g" > /etc/apt/sources.list
        break
    done
    # 更新镜像源
    echo "正在更新镜像源"
    apt-get update
}

download_important_software() {
    echo "正在下载必要软件...." >&2
    echo apt-get install -y ${all_needed_software[@]} >&2
    apt-get install -y ${all_needed_software[@]}
    echo "正在更新apt-file...." >&2
    apt-file update
}

download_self() {
    rm -rf new-linux-dist-config
    git clone https://gitee.com/mindulmindul/all_my_config.git
}

config_samba() {
    cp /etc/samba/smb.conf /etc/samba/smb.conf.$(date +%d%H%M).bak
    sed -n '1,/valid users/p' ./smb_user.txt  >> /etc/samba/smb.conf
    # mindul不需要密码
    echo 执行smbpasswd -a mindul 请输入密码
    smbpasswd -a mindul
    service smbd restart
    service nmbd restart
    ifconfig
    echo "  请您使用合适的ip地址，与本机器建立samba链接 \\\\ \\mindul"
}

download_my_config() {
    # 为下载配置仓库做好准备
    # 从gitee下载本仓库
    download_self

}

main() 
{
    if  [[ $(id -u) -ne 0 ]]; then
        echo you should execute $0 with root permission >&2
        exit 2
    fi

    if [[ ! -d .git ]] || [[ ! -d configs/apt_source_list_template ]]; then
        # 实在是没有办法了，镜像源和mirror是必须设置好的
        __change_apt_source_list
        # 从gitee下载本仓库
        download_self
        cd all_my_config
        exec ./install.sh
    fi

    # 修改apt镜像源
    echo 正在修改apt镜像源...
    change_apt_source_list

    # 下载samba
    echo 正在下载samba..
    apt-get install -y samba samba-common net-tools

    # 开始下载 openssh-server vim zsh等
    download_important_software >/dev/null &
    bak_pid=$!

    # 配置samba服务，使能文件共享，创建共享目录
    echo 正在配置samba...
    config_samba
    chown -R mindul:mindul .

    wait $bak_pid
}

main




